﻿using System;
using System.ComponentModel;
using Castle.ActiveRecord;

namespace MyMovieCollection.DATA.Models.Converter
{
    public class ARConverter<T> : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value.GetType() == typeof(string))
            {
                return ActiveRecordMediator.FindByPrimaryKey(typeof(T), Int32.Parse(value.ToString()));
            }
            return base.ConvertFrom(context, culture, value);
        }
    }
}